package com.me.modules.assign.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.controller.BaseController;
import com.me.modules.common.page.Page;
import com.me.modules.common.utils.*;
import com.me.modules.sys.entity.FieldDictionaryEntity;
import com.me.modules.sys.entity.OperationEntity;
import com.me.modules.sys.entity.ValueDictionaryEntity;
import com.me.modules.sys.service.FieldDictionaryService;
import com.me.modules.sys.service.OperationService;
import com.me.modules.sys.service.ValueDictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.me.modules.assign.entity.TskAssignInfoEntity;
import com.me.modules.assign.service.TskAssignInfoService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * 任务流程
 *
 * @author me
 * @date 2019-01-14 10:56:27
 */
@RequestMapping("/assign")
@Controller
public class TskAssignInfoController extends BaseController{
	@Autowired
	private TskAssignInfoService tskAssignInfoService;
	@Autowired
	private FieldDictionaryService fieldDictionaryService;
	@Autowired
	private ValueDictionaryService valueDictionaryService;
	@Autowired
	private OperationService operationService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("tskassigninfo:all")
	@SysLog("查看任务工单列表")
	public String list(TskAssignInfoEntity tskAssignInfoEntity, Model model , HttpServletRequest request){
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<TskAssignInfoEntity> page = tskAssignInfoService.findPage(tskAssignInfoEntity, pageNum);

		List<TskAssignInfoEntity>  rlsList = page.getResult();
		List<TskAssignInfoEntity>  newRlsList = new ArrayList<>();
		List<FieldDictionaryEntity> fieldList = fieldDictionaryService.queryByTableName("assign");
		if(fieldList.size()>0){
			if( rlsList.size()>0){
				for (TskAssignInfoEntity info : rlsList) {
					String tableId = info.getId().toString();
					List<ValueDictionaryEntity> valueList = new ArrayList<>();
					for (FieldDictionaryEntity fieldDictionaryEntity : fieldList) {
						Long fieldID = fieldDictionaryEntity.getId();
						ValueDictionaryEntity value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
						if(value!=null){
							valueList.add(value);
						}else{
							valueList.add(new ValueDictionaryEntity());
						}
					}
					info.setValueDictionarys(valueList);
					newRlsList.add(info);
				}
			}
			model.addAttribute("page",new Page(newRlsList,page.getTotal(),page.getPageSize() , page.getPageNum()));

		}else{
			model.addAttribute("page",page);
		}

		model.addAttribute("assign",tskAssignInfoEntity);
		model.addAttribute("fieldList",fieldList);

		return "assign/assign";
	}


	/**
	 * 工单信息
	 * @param model
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/info")
	@RequiresPermissions("tskassigninfo:all")
	@SysLog("查看任务工单信息")
	public String info(Model model , String id, HttpServletRequest request){
		List<FieldDictionaryEntity> fieldList = fieldDictionaryService.queryByTableName("assign");
		if(!StringUtils.isEmpty(id)){
			TskAssignInfoEntity info = tskAssignInfoService.queryObject(Long.valueOf(id));
			if(fieldList != null && fieldList.size() > 0){
				String tableId = info.getId().toString();
				List<ValueDictionaryEntity> valueList = new ArrayList<>();
				for (FieldDictionaryEntity fieldDictionaryEntity : fieldList) {
					Long fieldID = fieldDictionaryEntity.getId();
					ValueDictionaryEntity value = valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
					if(value != null){
						valueList.add(value);
					}else{
						valueList.add(new ValueDictionaryEntity());
					}
				}
				info.setValueDictionarys(valueList);
			}

			model.addAttribute("assign",info);
		}
		model.addAttribute("fieldList",fieldList);
		//查询业务系统
		List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
		model.addAttribute("operationTree", JsonUtil.getJsonByObj(operationList));
		return "assign/assignEdit";
	}


	/**
	 * 新建工单/编辑工单
	 * @param tskAssignInfoEntity
	 * @return
	 */
	@RequestMapping(value = "edit",method = RequestMethod.POST)
	@RequiresPermissions("tskassigninfo:all")
	@ResponseBody
	public Result edit(TskAssignInfoEntity tskAssignInfoEntity){
		if(StringUtils.isEmpty(tskAssignInfoEntity.getId())){
			try {
				tskAssignInfoEntity.setAssignCode(Utils.getSN("TSK_","yyyyMMddHHmmss",2));
				tskAssignInfoEntity.setStatus("0");
				tskAssignInfoEntity.setReqId(UserUtils.getCurrentUser().getId());
				tskAssignInfoEntity.setReqName(UserUtils.getCurrentUser().getUserName());
				tskAssignInfoEntity.setAssignCreateDate(new Date());
				tskAssignInfoService.save(tskAssignInfoEntity);
				if(tskAssignInfoEntity.getValueDictionarys().size()>0) {
					for (ValueDictionaryEntity value : tskAssignInfoEntity.getValueDictionarys()) {
						if(!StringUtils.isEmpty(value.getFieldValue())){
							value.setTableId(tskAssignInfoEntity.getId().toString());
							valueDictionaryService.save(value);
						}
					}
				}
				return Result.ok("新建任务工单成功!");
			} catch (Exception e) {
				e.printStackTrace();
				return Result.error("新建任务工单失败!请联系管理员");
			}
		}else {
			try {
				tskAssignInfoService.update(tskAssignInfoEntity);
				if(tskAssignInfoEntity.getValueDictionarys().size()>0) {
					for (ValueDictionaryEntity value : tskAssignInfoEntity.getValueDictionarys()) {
						ValueDictionaryEntity valueDictionaryEntity =
								valueDictionaryService.findByFieldIdAndUserId(value.getFieldId(),tskAssignInfoEntity.getId().toString());
						if(valueDictionaryEntity!=null){
							valueDictionaryService.delete(valueDictionaryEntity.getId());
						}
						if(!StringUtils.isEmpty(value.getFieldValue())){
							value.setTableId(tskAssignInfoEntity.getId().toString());
							valueDictionaryService.save(value);
						}
					}
				}
				return Result.ok("修改任务工单成功!");
			} catch (Exception e) {
				e.printStackTrace();
				return Result.error("修改任务工单失败!请联系管理员");
			}
		}
	}


	/**
	 * 删除工单
	 */
	@RequestMapping(value = "delete",method = RequestMethod.POST)
	@RequiresPermissions("tskassigninfo:all")
	@ResponseBody
	@SysLog("删除任务工单")
	public Result delete(String id){
		try {
			if(tskAssignInfoService.delete(Long.valueOf(id)) < 1){
				return Result.error("删除任务工单失败!请联系管理员");
			}
			return Result.ok("删除任务工单成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("删除任务工单失败!请联系管理员");
		}
	}


}
